﻿//Copyright 2010 http://tv4home.codeplex.com
//This file is part of TV4Home.WebInterface.UI.
//TV4Home.WebInterface.UI is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
//TV4Home.WebInterface.UI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU General Public License along with TV4Home.WebInterface.UI If not, see http://www.gnu.org/licenses/
using System;
using System.Reflection;
using System.Resources;
using System.Web.Compilation;
using System.Web;
using System.Globalization;

namespace TV4Home.WebInterface.UI.Code
{
    internal class SatelliteResourceProviderFactory : ResourceProviderFactory
    {
        #region Constructor
        public SatelliteResourceProviderFactory()
        {
        }
        #endregion

        #region Methods
        public override IResourceProvider CreateGlobalResourceProvider(string classKey)
        {
            return new GlobalSatelliteResourceProvider(classKey);
        }

        public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
        {
            return new LocalSatelliteResourceProvider(virtualPath);
        }
        #endregion
    }

    internal class GlobalSatelliteResourceProvider : IResourceProvider
    {
        #region Fields
        private ResourceManager _resourceManager;
        private string _classKey;
        #endregion

        #region Constructor
        internal GlobalSatelliteResourceProvider(string classKey)
        {
            this._classKey = classKey;
        }
        #endregion

        #region Methods
        private ResourceManager CreateResourceManager()
        {
            Assembly currentAssembly = Assembly.GetExecutingAssembly();

            string baseName = currentAssembly.GetName().Name + ".App_GlobalResources." + this._classKey;

            ResourceManager resMgr = new ResourceManager(baseName, Assembly.GetExecutingAssembly());
            resMgr.IgnoreCase = true;

            return resMgr;
        }
        #endregion

        #region IResourceProvider Member
        public object GetObject(string resourceKey, CultureInfo culture)
        {
            if (this._resourceManager == null)
                this._resourceManager = CreateResourceManager();

            return this._resourceManager.GetObject(resourceKey, culture ?? CultureInfo.CurrentUICulture);
        }

        public IResourceReader ResourceReader
        {
            get { return null; }
        }
        #endregion
    }

    internal class LocalSatelliteResourceProvider : IResourceProvider
    {
        #region Fields
        private ResourceManager _resourceManager;
        private string _virtualPath;
        #endregion

        #region Constructor
        internal LocalSatelliteResourceProvider(string virtualPath)
        {
            this._virtualPath = VirtualPathUtility.ToAppRelative(virtualPath).Replace("~", "");
        }
        #endregion

        #region Methods
        private ResourceManager CreateResourceManager()
        {
            Assembly currentAssembly = Assembly.GetExecutingAssembly();

            string baseName = currentAssembly.GetName().Name + VirtualPathUtility.GetDirectory(this._virtualPath).Replace('/', '.') + "App_LocalResources." + VirtualPathUtility.GetFileName(this._virtualPath);

            ResourceManager resMgr = new ResourceManager(baseName, currentAssembly);
            resMgr.IgnoreCase = true;

            return resMgr;
        }
        #endregion

        #region IResourceProvider Member
        public object GetObject(string resourceKey, CultureInfo culture)
        {
            if (this._resourceManager == null)
                this._resourceManager = CreateResourceManager();

            return this._resourceManager.GetObject(resourceKey, culture ?? CultureInfo.CurrentUICulture);
        }

        public IResourceReader ResourceReader
        {
            get { return null; }
        }
        #endregion
    }
}
